package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author BAGUS
 */
public class GasElpiji {

    private String jenis;
    private double jumlah;
    private double harga;
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
    String userid = "hr"; // gantikan dengan username anda
    String pass = "hr"; // gantikan dengan password anda

    public GasElpiji() {
    }

    public double getHarga() {
        return harga;
    }

    public String getJenis() {
        return jenis;
    }

    public double getJumlah() {
        return jumlah;
    }

    public void setHarga(double harga) {
        this.harga = harga;
    }

    public void setJenis(String jenis) {
        this.jenis = jenis;
    }

    public void setJumlah(double jumlah) {
        this.jumlah = jumlah;
    }

    public void tambahGasElpiji() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select jumlahgaselpiji from gaselpiji where jenisgaselpiji = '" + getJenis() + "'");
            double jumlahgaselpiji = 0;
            while (resultSet.next()) {
                jumlahgaselpiji = resultSet.getDouble("jumlahgaselpiji");
            }
            double totalgaselpiji = jumlahgaselpiji + getJumlah();
            statement.executeUpdate("update gaselpiji set jumlahgaselpiji = " + totalgaselpiji + " where jenisgaselpiji = '" + getJenis() + "'");
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }

    public void kurangGasElpiji() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select jumlahgaselpiji from gaselpiji where jenisgaselpiji = '" + jenis + "'");
            double jumlahgaselpiji = 0;
            while (resultSet.next()) {
                jumlahgaselpiji = resultSet.getDouble("jumlahgaselpiji");
            }
            double totalgaselpiji = jumlahgaselpiji - jumlah;
            statement.executeUpdate("update gaselpiji set jumlahgaselpiji = " + totalgaselpiji + " where jenisgaselpiji = '" + jenis + "'");
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }

    public void updateHargaGasElpiji() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            statement.executeUpdate("update gaselpiji set hargagaselpiji = "
                    + getHarga() + " where jenisgaselpiji = '"
                    + getJenis() + "'");
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }

    public ResultSet tampilGasElpiji() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from gaselpiji");
            return resultSet;
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return null;
        }
    }

    public boolean bandingJumlahGasLPG(double jumlah, String jenis) {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select jumlahgaselpiji from gaselpiji where jenisgaselpiji = '" + jenis + "'");
            String jumlah1 = "0";
            while (resultSet.next()) {
                jumlah1 = resultSet.getString("jumlahgaselpiji");
            }
            double jumlah2 = Double.parseDouble(jumlah1);
            if (jumlah <= jumlah2) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            return false;
        }
    }
}
